package com.nms.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.nms.handler.SystemInfoHandler;
import com.nms.model.Role;
import com.nms.model.User;
import com.nms.model.pojo.SystemInfo;
import com.nms.service.UserService;
import com.nms.util.UserLoginUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserManageAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private SystemInfo systemInfo;
	private User user;
	private List<User> users;

	private UserService userService;

	public UserManageAction(){
		systemInfo = new SystemInfo();
		SystemInfoHandler.getInstance().copySystemInfo(systemInfo);
	}
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public SystemInfo getSystemInfo() {
		return systemInfo;
	}

	public void setSystemInfo(SystemInfo systemInfo) {
		this.systemInfo = systemInfo;
	}

	// ////////////////////////////////////////
	public String delete() {
		return "deleteSuccess";
	}

	public String showAdd() {
		if (UserLoginUtil.getInstance().isExceed())
			return "exceed";
		return "showAdd";
	}

	public String update() {
		return "updateSuccess";
	}

	@Override
	public String execute() {
		/* 判断用户是否登录以及用户权限*/
		if (isExceeded())
			return "exceed";
		/* 获得HttpServletRequest对象*/
		HttpServletRequest hsr = ServletActionContext.getRequest();
		/* 获得请求的参数*/
		String id = hsr.getParameter("id");
		String act = hsr.getParameter("act");
		/* 下面为操作逻辑*/
		if (act.equals("delete") && userService.deleteByUsername(id)) {
			/* 为用户管理显示用户列表*/
			users = userService.findAll();
			return "success";
		} else if(act.equals("add")){
			/* 为添加的用户对象设置不可手动修改的属性值*/
			Role role = new Role(5,"普通用户","系统的一般用户");
			user.setRole(role);
			user.setGrade(0);
			user.setProperty(0);
			user.setHomepage("http:www.baidu.com");
			user.setRegisterTime(new Date());
			user.setState(1);
			if(userService.register(user) == 1){
				/* 为用户管理显示用户列表*/
				users = userService.findAll();
				return "success";
			}
		} else if (act.equals("update")) {
			String detail = hsr.getParameter("detail");
			if (detail != null && detail != "") {
				/* 从数据库中查找出要更新的用户对象*/
				user = userService.find(id);
				if (detail.equals("resetPw") && userService.resetPw(id)) {
					/* 为用户管理显示用户列表*/
					users = userService.findAll();
					return "success";
				} else if(detail.equals("more")){
					/* 更新用户的相关信息*/
					user.setState(Integer.parseInt(hsr.getParameter("state")));
					Role r = new Role();
					r.setId(Integer.parseInt(hsr.getParameter("role")));
					user.setRole(r);
					userService.update(user);
					/* 为用户管理显示用户列表*/
					users = userService.findAll();
					return "success";
				}
			} else {
				/* 从数据库中查找出要更新的用户对象*/
				User u = userService.find(id);
					/* 更新用户的相关信息*/
				user.setId(u.getId());
				user.setGrade(u.getGrade());
				user.setRegisterTime(u.getRegisterTime());
				userService.update(user);

				/* 为用户管理显示用户列表*/
				users = userService.findAll();
				return "success";
			}
		} else if(act.equals("edit")){
			/* 从数据库中找出要进行编辑的用户对象*/
			user = userService.find(id);
			if(user != null)
				return "edit";
		} 
		return "error";
	}

	// //////////////////////////////
	private boolean isExceeded() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		User u = (User) session.get("user");
		if(u.getRole().getId() == 5)
			return true;
		return (u == null);
	}
}
